#include "itemmodel.h"

#include <QDebug>

ItemModel::ItemModel(QObject *parent)
    : QAbstractListModel(parent)
{
    QHash<int, QByteArray> roles;

    roles[RoleItemName] = "name";
    roles[RoleItemId] = "itemId";
    roles[RoleGroupId] = "groupId";
    roles[RoleGroupName] = "groupName";
    roles[RoleIndex] = "index";
    roles[RoleIsAGroup] = "isAGroup";
    roles[RoleGroupNumber] = "groupNumber";

    setRoleNames(roles);
}

/*
void ListModel::addListItem(const ElementPopisa &elementPopisa, bool isAGroup, int groupNumber, int index){
    beginInsertRows(QModelIndex(), rowCount(), rowCount());
    m_listItems<<new ListItem(elementPopisa.dajId(),elementPopisa.dajArtikl(),elementPopisa.dajKolicinu(),
                              elementPopisa.jeKupljen(),elementPopisa.dajMjernuJednincu(), isAGroup, groupNumber, index);

    qDebug()<<"dodajem element"<<elementPopisa.dajArtikl().dajImeArtikla();
    endInsertRows();
    emit countChanged(m_listItems.count());
}
  */


void ItemModel::addItem(const Artikl &item, int index, bool isAGroup, int groupCount){
    beginInsertRows(QModelIndex(), rowCount(), rowCount());
    m_Items<<new ItemItem(item.dajId(), item.dajImeArtikla(), item.dajGrupu(), index, isAGroup, groupCount);

    connect(m_Items.last(), SIGNAL(dataChanged()), SLOT(handleItemChange()));

    qDebug()<<"dodajem artikl"<<item.dajImeArtikla();

    endInsertRows();
    emit countChanged(m_Items.count());
}

int ItemModel::rowCount(const QModelIndex & parent) const {
    return m_Items.count();
}

int ItemModel::count(){
    return m_Items.count();
}

void ItemModel::clear(){
    int currentCount=m_Items.count();
    for (int i=0; i<currentCount; i++)
    {
        beginRemoveRows(QModelIndex(), 0, 0);

        ItemItem* item=m_Items.takeFirst();
        delete item;

        emit countChanged(m_Items.count());
        endRemoveRows();
    }
}

ItemItem* ItemModel::get(int index){
    return m_Items[index];
}


QVariant ItemModel::data(const QModelIndex & index, int role) const  {
    if (index.row() < 0 || index.row() > m_Items.count())
        return QVariant();

    ItemItem* item=m_Items[index.row()];

    if (role == RoleItemName)
        return item->name();
    else if (role == RoleItemId)
        return item->itemId();
    else if (role == RoleGroupId)
        return item->groupId();
    else if (role == RoleGroupName)
        return item->groupName();
    else if (role == RoleIndex)
        return item->index();
    else if (role == RoleIsAGroup)
        return item->isAGroup();
    else if (role == RoleGroupNumber)
        return item->groupNumber();

    return QVariant();
}
QModelIndex ItemModel::indexFromItem(const ItemItem *item) const
{
    Q_ASSERT(item);
    for(int row=0; row<m_Items.size(); ++row) {
        if(m_Items.at(row) == item) return index(row);
    }
    return QModelIndex();
}

void ItemModel::handleItemChange()
{
    ItemItem* item = static_cast<ItemItem*>(sender());
    QModelIndex index = indexFromItem(item);
    if(index.isValid())
        emit dataChanged(index, index);
}



